<?php 
   namespace app\index\controller;
   use \think\Cookie;
   class Index extends Base{
       function index(){
       	     $this->assign(['user'=>Cookie::get('user')]);
	         return $this->fetch('index');
	   }

	  //提交登入
	  function login(){
	  	 $user=input('post.');
	  	 $res=$this->redis->hgetall('user:'.$user['user']);
	  	 if($res){
	  	   if($user['pass']==$res['pass']){
               Cookie::set('user',$res['user']);
	  	 	   echo JSON_encode(['status'=>'ok']);
	  	   }else{
	  	 	  echo JSON_encode(['statsu'=>'fail','error'=>'账号或密码错误！']);
	  	  }
	     }else{echo JSON_encode(['status'=>'fail','error'=>'账号不存在！']);}
	  }

      //提交注册
	  function register(){
	  	 $user=input('post.');
	  	 $nameid=$this->redis->sadd('userid',$user['user']);
	  	 if(!$nameid){echo JSON_encode(['status'=>'fail','error'=>'账号已存在！']);die();}
	     $user=$this->redis->hmset('user:'.$user['user'],input('post.'));
	     if($user){
             echo JSON_encode(['status'=>'ok']);
	     }else{
	     	 echo JSON_encode(['status'=>'fail','error'=>'系统繁忙请稍后注册！']);
	     }
	  }

   }
?>